<!DOCTYPE html>
<html >
<head>
    <meta charset="UTF-8" />
    <title>菜单及权限</title>
    <#include "/common/baseHead.ftl">
</head>
<body>

<div id="app" style="padding: 20px;"  v-cloak>
    <!--工具条-->
    <el-col :span="24" class="toolbar" >
        <el-form :inline="true" :model="queryParams">
            <el-form-item>
                <el-input v-model="queryParams.name" placeholder="权限名称"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="getData">搜索</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="handleNew">新增</el-button>
            </el-form-item>
        </el-form>
    </el-col>


    <transition name="slide-fade">

        <el-table
                :data="tableData"
                v-loading="loading"
                border
                style="width: 100%;">
            <el-table-column
                    label="ID"
                    width="180">
                <template slot-scope="scope">
                    <template v-if="scope.row.nodeCode.length === 3">
                        &nbsp;&nbsp;
                    </template>
                    <template v-else-if="scope.row.nodeCode.length === 6">
                        &nbsp;&nbsp; &nbsp;&nbsp;
                    </template>
                    <template v-else-if="scope.row.nodeCode.length === 9">
                        &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;
                    </template>
                    <i v-if="!scope.row.isLeaf&&!scope.row.children" class="el-icon-caret-bottom"
                       style="cursor: pointer;" @click="collapse(scope.$index,scope.row)"></i>
                    <i v-else-if="!scope.row.isLeaf&&scope.row.children" class="el-icon-caret-right"
                       style="cursor: pointer;" @click="expand(scope.$index,scope.row)"></i>
                    <i v-else class="fa fa-square-o"></i>
                    <span style="margin-left: 10px">{{ scope.row.id }}</span>
                </template>
            </el-table-column>
            <el-table-column
                    prop="name"
                    label="权限名"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="url"
                    label="链接"
                    width="380">
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="handleCreate(scope.$index, scope.row)">添加子权限
                    </el-button>
                    <el-button
                            size="mini"
                            @click="handleEdit(scope.$index, scope.row)">编辑
                    </el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.$index, scope.row)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
    </transition>


    <el-dialog
            :title="titleFilter"
            :visible.sync="dialogVisible"
            width="80%"
    >
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">

            <el-form-item label="权限名" prop="name">
                <template v-if="mode==='see'">
                    <span class="see-span" :title="ruleForm.name"> {{ruleForm.name}}</span>
                </template>
                <template v-else>
                    <el-input  v-model="ruleForm.name"  placeholder="请输入名称" ></el-input>
                </template>
            </el-form-item>
            <el-form-item label="优先级" prop="listNo">
                <el-input v-model="ruleForm.listNo"></el-input>
            </el-form-item>
            <el-form-item label="url" prop="url">
                <el-input v-model="ruleForm.url"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="submitting" @click="submitForm('ruleForm')">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>


</div>
</body>
</html>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            tableData: [],
            dialogVisible: false,
            dialogData: null,
            loading: false,
            title: '权限',
            mode: 'list',
            saveUrl: '/admin/permission/insert',
            deleteUrl: '/admin/permission/delete',
            listUrl: '/admin/permission/tree',
            submitting: false,
            rules: {
                name: [
                    {required: true, message: '必填', trigger: 'blur'}
                ],
                type: [
                    {required: true, message: '必填', trigger: 'blur'}
                ]
            },
            ruleForm: {
                id: '',
                nodeFatherId: '',
                name: '',
                type: '',
                menuType: '',
                openMethod: '',
                note: '',
                url: '',
                orderNum: '',
                isLeaf: '',
                icon: ''
            },
            queryParams: {nodeN:''}
        }, methods: {
            submitForm(formName) {
                var that = this;
                this.$refs[formName].validate(function (valid) {
                    if (valid) {
                        that.submitting = true;
                        post(that.saveUrl, that.ruleForm,that).then(function (response) {
                            var result = response.data.code;
                            that.submitting = false;
                            if(result===0){
                                that.getData();
                                that.dialogVisible=false;
                            }

                        });
                    } else {
                        return false;
                    }
                });

            },
            handleNew(index, data){
                var cp={
                    id: '',
                    name: '',
                    type: '',
                    menuType: '',
                    openMethod: '',
                    note: '',
                    url: '',
                    icon: ''
                };
                this.ruleForm =cp;
                this.dialogVisible = true;
                this.mode = "create";
            },
            handleCreate(index, data){
                var cp={
                    id: '',
                    name: '',
                    type: '',
                    menuType: '',
                    openMethod: '',
                    note: '',
                    url: '',
                    icon: ''
                };
                cp.menuType=data.menuType;
                cp.nodeFatherId=data.id;
                this.ruleForm =cp;
                this.dialogVisible = true;
                this.mode = "create";
            },
            handleEdit(index, data){
                this.ruleForm={};
                for (var prop in data) {
                    this.$set(this.ruleForm, prop, data[prop]);
                }
                this.dialogVisible = true;
                this.mode = "edit";
            }, handleDelete(index, data){
                var that=this;
                this.$msgbox({
                    title: '提醒',
                    message:'确定要删除么？',
                    showCancelButton: false,
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    callback:function(action, instance){
                        if(action==='confirm'){
                            post(that.deleteUrl,data,that).then(function (response) {
                                var result = response.data.code;
                                if(result===0){
                                    that.getData();
                                }

                            });
                        }
                    }
                });

            }, collapse(index, data){
                let children = [];
                let length = 0;
                for (let i = index + 1; i < this.tableData.length; i++) {
                    if (this.tableData[i].nodeFatherId === data.nodeFatherId || this.tableData[i].nodeCode.length < data.nodeCode.length) {
                        break;
                    } else {
                        children.push(this.tableData[i]);
                        length++;
                    }
                }
                if (length > 0) {
                    this.tableData.splice(index + 1, length);
                    this.tableData[index].children = children;
                }
            }, expand(index, data){
                let children = data.children;
                this.tableData[index].children = null;
                let tell = this.tableData.splice(index + 1, this.tableData.length);
                let head = this.tableData.splice(0, index + 1);
                this.tableData = head.concat(children).concat(tell);
            }, getData(){
                this.loading = true;
                var that=this;
                get(that.listUrl, this.queryParams,this).then(function (response) {
                    that.tableData = response.data.data;
                    that.show = true;
                    that.loading = false;
                });
            }
        },
        computed: {
            titleFilter(){
                if (this.mode === 'create') {
                    return this.title + '新增';
                } else if (this.mode === 'edit') {
                    return this.title + '修改';
                } else if (this.mode === 'see') {
                    return this.title + '查看';
                }else{
                    return this.title + this.mode;
                }
            }
        },
        filters: {},
        mounted: function () {
            let that = this;
            let loading = this.$loading({
                lock: true
            });
            require(["${base}/plugins/qs/qs.js"], function (qs) {
                get(that.listUrl, that.queryParams,that).then(function (response) {
                    that.tableData = response.data.data;
                    console.log(that.tableData);
                    that.loading = false;
                    loading.close();
                });
            });

        }
    })


</script>